package com.qf.android.huihui.adapter.show;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.qf.android.huihui.R;
import com.qf.android.huihui.bean.show.ShowBean;
import com.squareup.picasso.Picasso;

import java.util.List;
public class ShowAdapter extends RecyclerView.Adapter<ShowAdapter.ViewHolder> {

    private List<ShowBean.DataBean.ListBean> list;
    private Context context;
    private MyItemClickListener mItemClickListener;

    public ShowAdapter(List<ShowBean.DataBean.ListBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_show,parent,false);
        ViewHolder holder=new ViewHolder(view,mItemClickListener);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
//        Log.d("info","----------"+list.size());
//        Log.d("info","------------"+list.get(position).getShaidanBean().hashCode());
        if (holder!=null) {
            holder.tv1.setText(list.get(position).getShaidan().getEditor().getNickname());
            holder.tv2.setText(list.get(position).getShaidan().getTitle());
            holder.tv3.setText(list.get(position).getShaidan().getSubTitle());
            Picasso.with(context).load(list.get(position).getShaidan().getImageUrl()).into(holder.big_iv);
            Glide.with(context).load(list.get(position).getShaidan().getEditor().getAvatar()).asBitmap().centerCrop().into(new BitmapImageViewTarget(holder.little_iv) {
                @Override
                protected void setResource(Bitmap resource) {
                    RoundedBitmapDrawable circularBitmapDrawable =
                            RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                    circularBitmapDrawable.setCircular(true);
                    holder.little_iv.setImageDrawable(circularBitmapDrawable);
                }
            });

         //   Picasso.with(context).load(list.get(position).getShaidan().getEditor().getAvatar()).into(holder.little_iv);
//           // holder.tv1.setText(list.get(position).getShaidanBean().getEditor().getNickname());
//            holder.tv2.setText(list.get(position).getShaidanBean().getTitle());
//            holder.tv3.setText(list.get(position).getShaidanBean().getSubTitle());
//            Picasso.with(context).load(list.get(position).getShaidanBean().getImageUrl()).into(holder.big_iv);
//            Picasso.with(context).load(list.get(position).getShaidanBean().getEditor().getAvatar()).into(holder.little_iv);
        }else{
            Toast.makeText(context, ""+"为空", Toast.LENGTH_SHORT).show();
        }


    }

    @Override
    public int getItemCount() {
      //  Log.d("info","size=="+list.size());
        return list.size();
    }
    /**
     * 实现OnClickListener接口重写的方法
     * @param
     */


    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView big_iv, little_iv;
        private TextView tv1, tv2, tv3;
        private MyItemClickListener mListener;
        public ViewHolder(View itemView,MyItemClickListener mItemClickListener) {
            super(itemView);
            this.mListener=mItemClickListener;
            big_iv = (ImageView) itemView.findViewById(R.id.show_big_iv);
            little_iv= (ImageView) itemView.findViewById(R.id.show_litle_iv);
            tv1= (TextView) itemView.findViewById(R.id.show_tv1);
            tv2= (TextView) itemView.findViewById(R.id.show_tv2);
            tv3= (TextView) itemView.findViewById(R.id.show_tv3);
            itemView.setOnClickListener(this);


        }
        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.onItemClick(v,getPosition());
            }

        }
    }
    /**
     * 创建一个回调接口
     */
    public interface MyItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setItemClickListener(MyItemClickListener myItemClickListener) {
        this.mItemClickListener = myItemClickListener;
    }

}
